/**
 * @FileName: PaymentService.java
 * @Author
 * @Description:
 * @Date 2017年3月10日 上午11:30:14
 * @CopyRight ZTE Corporation
 */
package cn.hpclub.server.service;

import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

import cn.hpclub.server.constant.ClientApiConstant;
import cn.hpclub.server.util.SystemConfigUtil;

public class PaymentService{
    public static final int      PAYMENT_ALIPAY      = 14;                  // 支付宝
    public static final int      PAYMENT_CCB         = 15;                  // 建行支付
    public static final int      PAYMENT_UNION_PAY   = 16;                  // 银联支付
    public static final int      PAYMENT_WECHAT      = 20;                  // 微信支付
    public static final int      PAYMENT_ABC         = 25;                  // 农行支付
    public static final int      MODULE_ID_CHONG_ZHI = 1;                   // 充值缴费模块
    public static final int      MODULE_ID_XUAN_HAO  = 3;                   // 选号入网模块

    public static PaymentService instance            = new PaymentService();

    public static PaymentService getInstance(){
        return instance;
    }

    private PaymentService(){}

    public JSONObject getPaymentList(int userId, int moduleId){
        List<Record> list = getPaymentListFromDb(userId, moduleId);
        return generateResult(list);
    }

    private JSONObject generateResult(List<Record> list){
        if(list.isEmpty()){
            return getResponse(ClientApiConstant.ResultCode.SUCCESS_CODE, ClientApiConstant.Msg.EMPTY,
                               ClientApiConstant.Msg.EMPTY);
        }
        JSONArray array = new JSONArray();
        for(int i = 0; i < list.size(); i++){
            Record pay = list.get(i);
            JSONObject obj = new JSONObject();
            obj.put(ClientApiConstant.Key.DEFAULT, i == 0 ? 1 : 0);
            obj.put(ClientApiConstant.Key.SORT, i + 1);
            obj.put(ClientApiConstant.Key.PAY_PLAT, Integer.parseInt(pay.getStr("code")));
            obj.put(ClientApiConstant.Key.PAY_NAME, getPaymentName(pay.getStr("name")));
            obj.put(ClientApiConstant.Key.PAY_IMG, getPaymentImageUrl(pay.getStr("img_uri")));
            array.put(obj);
        }
        return getResponse(ClientApiConstant.ResultCode.SUCCESS_CODE, ClientApiConstant.Msg.EMPTY, array);
    }

    private String getPaymentName(String paymentName){
        return paymentName == null ? "" : paymentName;
    }

    private String getPaymentImageUrl(String imgUri){
        String imgUrl = SystemConfigUtil.getSystemConfig().getHostPrefix() + imgUri;
        return imgUrl;
    }

    private List<Record> getPaymentListFromDb(int userId, int moduleId){
        String sql = "SELECT pay.id, pay.code, pay.name, pay.img_uri, pay.sort"
                + " FROM pay, pay_config pc, organization org WHERE pay.id = pc.pay_plat"
                + " AND org.id = ( SELECT org_id FROM member_org WHERE id =? )"
                + " AND org.enabled = 1 AND org.is_deleted = 0 AND org.CityCode = pc.city_org_id"
                + " AND org.WebsiteId = pc.website_id AND pc.module_id =? AND pay.enabled = 1"
                + " AND pay.is_deleted = 0 AND pay. CODE <> 15"
                + " UNION SELECT id, CODE, NAME, img_uri, sort FROM pay WHERE enabled = 1 AND is_deleted = 0 AND code = 15"
                + " ORDER BY sort ASC";
        return Db.find(sql, userId, moduleId);
    }

    public JSONObject getResponse(int code, String msg, Object data){
        JSONObject rjson = new JSONObject();
        rjson.put(ClientApiConstant.Key.RESULT_CODE, code);
        rjson.put(ClientApiConstant.Key.MESSAGE, msg);
        rjson.put(ClientApiConstant.Key.DATA, data);
        return rjson;
    }
}
